// /**
//  * @param {number[][]} obstacleGrid
//  * @return {number}
//  */
// var uniquePathsWithObstacles = function (obstacleGrid) {
//   let m = obstacleGrid.length
//   let n = obstacleGrid[0].length
//   let dp = Array(m)
//     .fill()
//     .map((item) => Array(n).fill(0))
//   for (let i = 0; i < m && obstacleGrid[i][0] == 0; i++) {
//     if (obstacleGrid[i][0] == 0) dp[i][0] = 1
//   }
//   for (let i = 0; i < n; i++) {
//     if (obstacleGrid[0][i] == 0) dp[0][i] = 1
//   }
//   // for (let i = 1; i < m; i++) {
//   //   for (let j = 1; j < n; j++) {
//   //     dp[i][j] = obstacleGrid[i][j] == 1 ? 0 : dp[i - 1][j] + dp[i][j - 1]
//   //   }
//   // }
//   console.log(dp)
//   return dp[m - 1][n - 1]
// }

// console.log(uniquePathsWithObstacles([[1, 0]]))
/**
 * @param {number[][]} obstacleGrid
 * @return {number}
 */
var uniquePathsWithObstacles = function (obstacleGrid) {
  let m = obstacleGrid.length
  let n = obstacleGrid[0].length
  let dp = new Array(m).fill(0).map(() => new Array(n).fill(0))
  dp[0][0] = obstacleGrid[0][0] ? 0 : 1
  for (let i = 1; i < m; i++) {
    dp[i][0] = dp[i - 1][0]==0 || obstacleGrid[i][0]==1 ? 0 : 1
  }
  for (let j = 1; j < n; j++) {
    dp[0][j] = dp[0][j - 1]==0 || obstacleGrid[0][j]==1 ? 0 : 1
  }
  for (let i = 1; i < m; i++) {
    for (let j = 1; j < n; j++) {
      dp[i][j] = obstacleGrid[i][j] == 1 ? 0 : dp[i - 1][j] + dp[i][j - 1]
    }
  }
  return dp[m - 1][n - 1]
}
uniquePathsWithObstacles([[0,0,0],[0,1,0],[0,0,0]])
